package com.yundong.gongchengji.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yundong.gongchengji.R;
import com.yundong.gongchengji.manager.ActivityManager;

import static com.yundong.gongchengji.R.id.imageLeft;

/**
 * 基础activity
 * Created by ls on 2017/2/13.
 */

public abstract class BaseActivity extends FragmentActivity{


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());


//        view.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        ActivityManager.getInstance().onCreate(this);
        initView(savedInstanceState);
        initVariable();
        processLogic(savedInstanceState);
    }
    /********************
     * 设置title;
     *********************/
    /**
     * title的左边设置
     */
    protected void setLeft() {
        ImageView left = (ImageView) findViewById(imageLeft);
        if (left == null)
            return;
        left.setVisibility(View.VISIBLE);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Log.e("测试","测试");
                finish();
            }
        });
    }
    protected void setLeft(int type) {
        ImageView left = (ImageView) findViewById(imageLeft);
        if (left == null)
            return;
        left.setVisibility(View.VISIBLE);
        left.setBackground(ContextCompat.getDrawable(this,type));
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Log.e("测试","测试");
                finish();
            }
        });
    }

    /**
     * title的右边设置
     * @param rightListener  上下文
     * @param img  图片文件
     */
    protected void setRight(View.OnClickListener rightListener,int img) {
        TextView right = (TextView) findViewById(R.id.textRight);
        if (right == null)
            return;
        right.setBackground(getResources().getDrawable(img));
        right.setVisibility(View.VISIBLE);
        right.setOnClickListener(rightListener);
    }

    protected void setRight(View.OnClickListener rightListener,String str){
        TextView right = (TextView) findViewById(R.id.textRight);
        if (right == null)
            return;
        right.setVisibility(View.VISIBLE);
        right.setOnClickListener(rightListener);
        right.setText(str);
    }

    /**
     * title的文字设置
     */
    protected void setTitle(String titleStr) {
        TextView title = (TextView) findViewById(R.id.label_title);
        if (title == null)
            return;
        title.setVisibility(View.VISIBLE);
        title.setText(titleStr);
    }
    protected void setTitleColor(String titleStr,int type) {
        TextView title = (TextView) findViewById(R.id.label_title);
        if (title == null)
            return;
        title.setVisibility(View.VISIBLE);
        title.setText(titleStr);
        title.setTextColor(ContextCompat.getColor(this,type));
    }
    /**
     * 设置title的背景样式
     */
    protected void setBackgroundStyle(int type){
        View view=findViewById(R.id.title_layout);
        view.setBackground(ContextCompat.getDrawable(this,type));
    }


    //设置根目录文件
    public abstract int getContentViewId();

    /**
     * 初始化布局以及View控件
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * 初始化变量
     */
    protected abstract void initVariable();

    /**
     * 处理业务逻辑，状态恢复等操作
     */
    protected abstract void processLogic(Bundle savedInstanceState);

    /**
     * 移除单个activity
     * @param activity
     */
    public void removeActivity(Class<?> activity){
        ActivityManager.getInstance().removeActivity(activity);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActivityManager.getInstance().onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActivityManager.getInstance().onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getInstance().onDestroy(this);
    }


    protected void showToast(Context context, String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
    }
}
